const path = require('path')
const fs = require('fs')
const xlsx = require('xlsx')

/**
 * 获取用户 Excel 文件的绝对路径
 * @returns {string} 文件的绝对路径
 */
function getFilePath(fileName) {
  return path.join(__dirname, `../xlsx/${fileName}.xlsx`)
}

function createExcelFileIfNotExists(filePath, name) {
  if (!fs.existsSync(filePath)) {
    const newWorkbook = xlsx.utils.book_new()
    const newSheet = xlsx.utils.json_to_sheet([])
    xlsx.utils.book_append_sheet(newWorkbook, newSheet, name)
    xlsx.writeFile(newWorkbook, filePath)
  }
}

module.exports = { getFilePath, createExcelFileIfNotExists }
